package com.mzy.basics.serializable;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author Ives
 * * @date 2022/2/16
 */
public class ObjectMainTest {

  public static void main(String[] args) throws Exception {
    //序列化
    serializeAnimal();
    //反序列化
    deserializeAnimal();
  }

  private static void serializeAnimal() throws Exception {
    Student black = new Student("张三", 20);
    System.out.println(black.toString());
    System.out.println("=================开始序列化================");
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.txt"));
    oos.writeObject(black);
    oos.flush();
    oos.close();
  }

  private static void deserializeAnimal() throws Exception {
    System.out.println("=================开始反序列化================");
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.txt"));
    Student black = (Student) ois.readObject();
    ois.close();
    System.out.println(black.toString());
  }
}